let routeStates, ngDepModules = [];

function loadBasicModules() {
  /*这里可以调整为CDN加快访问速度*/
  /*require('./lib/ionic/css/ionic.min.css');
   require('./lib/ionic/js/ionic.bundle.min');*/

  ngDepModules.push('ionic');
  ngDepModules.push('ui.router');

  require('./lib/ionic/js/angular/angular-resource.min');
  ngDepModules.push('ngResource');

  require('ocLazyLoad');
  ngDepModules.push('oc.lazyLoad');

  require('./lib/share/css/share.css');
  require('./lib/share/js/download_from_wechat.128');

  require('./css/style.css');

  require('./modules/base/app-fontSize');
}

loadBasicModules();

// define one angular module
const ngModule = angular.module('app', ngDepModules);

// 公共常量模型
require('./modules/base/app-constant')(ngModule);

// 公共变量模型
require('./modules/base/app-config')(ngModule);

// 公共服务模型
require('./modules/base/app-service')(ngModule);

// 公共原生模型
require('./modules/base/app-native')(ngModule);

ngModule.config(
  ($ionicConfigProvider, $stateProvider, $urlRouterProvider, $ocLazyLoadProvider) => {
    // ionic config
    $ionicConfigProvider.views.maxCache(10);
    $ionicConfigProvider.views.swipeBackEnabled(true);
    $ionicConfigProvider.scrolling.jsScrolling(true);

    // lazy load config
    $ocLazyLoadProvider.config({
      debug: true,
      events: true
    });

    // router config
    routeStates = require('./router')(angular);
    routeStates.forEach(state => {
      $stateProvider.state(state);
    });
    $urlRouterProvider.otherwise($injector => {
      $injector.get('$state').go('login');
    });
  }
);

ngModule.run($rootScope => {
  $rootScope.$on('$stateChangeStart', (event, toState, toParams, fromState) => {
    $rootScope.toState = toState;
    $rootScope.toStateParams = toParams;
    console.group('$stateChangeStart');
    console.log('fromState:' + fromState.name);
    console.log('toState:' + toState.name);
    console.groupEnd('$stateChangeStart');
  });

  $rootScope.$on('$stateChangeSuccess', (event, toState, toParams, fromState) => {
    console.group('$stateChangeSuccess');
    console.log('fromState:' + fromState.name);
    console.log('toState:' + toState.name);
    console.groupEnd('$stateChangeSuccess');
  });
});

